package com.atguigu.myjuc.mythread;

public class YThread {
   static class Condition{
        int number=0;
        public synchronized void increase(){
            while (number!=0){
                try {
                    wait(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

                System.out.println(Thread.currentThread().getName()+"当前为"+ number++);;

            notify();
        }
        public synchronized void decrease()  {
            while (number==0){
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

                System.out.println(Thread.currentThread().getName()+"当前为"+number--);

            notify();
        }
    }

    public static void main(String[] args) {
        Condition condition = new Condition();
        new Thread(()->{for (int i = 0; i < 20; i++) condition.increase();},"a").start();
        new Thread(()->{for (int i = 0; i < 20; i++) condition.decrease();},"b").start();
        new Thread(()->{for (int i = 0; i < 20; i++) condition.increase();},"c").start();
        new Thread(()->{for (int i = 0; i < 20; i++) condition.decrease();},"d").start();
    }
}
